package com.sky.controller.admin;

import com.sky.dto.CategoryPageQueryDTO;
import com.sky.dto.SetmealDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * 新增套餐
     */
    @PostMapping
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    @ApiOperation("新增套餐")
    public Result add(@RequestBody SetmealDTO setmealDTO){
        log.info("新增套餐,参数 ={}",setmealDTO);
        setmealService.insert(setmealDTO);

        return Result.success();
    }
    /**
     * 分页查询
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result page(CategoryPageQueryDTO pageQueryDTO){
        log.info("分页查询");
        PageResult pageResult = setmealService.page(pageQueryDTO);

        return Result.success(pageResult);
    }
    /**
     * 批量删除套餐
     */
    @ApiOperation("批量删除套餐")
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result deleteBySetmealIds(@RequestParam List<Long> setmealIds){
        log.info("批量删除套餐 ids={}",setmealIds);
        setmealService.deleteBatch(setmealIds);

        return Result.success();
    }

    /**
     * 根据id查询套餐信息
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐信息")
    public Result update(@PathVariable Long id){
        log.info("根据id查询套餐信息 id={}",id);
        SetmealVO setmealVO = setmealService.getById(id);

        return Result.success(setmealVO);
    }

    /**
     * 根据id修改套餐信息
     */
    @PutMapping
    @ApiOperation("修改套餐信息")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result getById(@RequestBody SetmealDTO setmealDTO){
        log.info("修改套餐信息 参数={}",setmealDTO);
        setmealService.updateWithDish(setmealDTO);

        return Result.success();
    }

    /**
     * 套餐起售停售
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("套餐起售停售")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result startOrStop(@PathVariable Integer status, Long id) {
        setmealService.startOrStop(status, id);
        return Result.success();
    }
}
